/**
 * 
 */
package fr.toussna.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

import fr.toussna.entity.Category;
import fr.toussna.entity.Member;
import fr.toussna.exception.ElementNotExistException;
import fr.toussna.utils.EntityFactory;

/**
 * Testing AccountService
 * @author My
 *
 */

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Test(suiteName = "AccountServiceTestCase")
public class AccountServiceTestCase extends AbstractTestNGSpringContextTests {

	private static final Log	LOGGER	= LogFactory
	                                           .getLog(AccountServiceTestCase.class
	                                                   .getName());

	@Autowired
	@Qualifier("accountservice")
	private AccountService	     accountservice;
	
	@Autowired
	@Qualifier("entityfactory")
	private EntityFactory	 entityfactory;
	
	
	
	
	@Test
	public void createMember() {
		LOGGER.info("createMember - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Member member2 = accountservice.createMember(member);
		Assert.assertNotNull(member2);
		Assert.assertEquals(member.getEmail(), member2.getEmail());
		Assert.assertEquals(member.getPassword(), member2.getPassword());
		Assert.assertEquals(member.getName(), member2.getName());
		accountservice.deleteMember(member2);
		LOGGER.info("createMember - end.");
	}
	
	@Test
	public void findMember() {
		LOGGER.info("findMember - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Member member2 = accountservice.createMember(member);
		Member member3;
		try {
			member3 = accountservice.findMember(member2.getId());

			Assert.assertNotNull(member3);
			Assert.assertEquals(member.getEmail(), member3.getEmail());
			Assert.assertEquals(member.getPassword(), member3
			        .getPassword());
			Assert.assertEquals(member.getName(), member3
			        .getName());
			accountservice.deleteMember(member3);
		} catch (ElementNotExistException e) {
		}
		LOGGER.info("findMember - end.");
	}
	
	@Test
	public void updateMember() {
		LOGGER.info("updateMember - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Member member2 = accountservice.createMember(member);
		member2.setEmail("changed");
		member2.setPassword("changed");
		member2.setName("changed");
		accountservice.updateMember(member2);
		Member member3;
		try {
			member3 = accountservice.findMember(member2.getId());
			Assert.assertNotNull(member3);
			Assert.assertEquals("changed", member3.getEmail());
			Assert.assertEquals("changed", member3.getPassword());
			Assert.assertEquals("changed", member3.getName());
			accountservice.deleteMember(member2);
		} catch (ElementNotExistException e) {
		}
		LOGGER.info("updateMember - end.");
	}
	
	@Test(expectedExceptions = { ElementNotExistException.class })
	public void deleteMember() throws ElementNotExistException {
		LOGGER.info("deleteMember - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Member member2 = accountservice.createMember(member);
		accountservice.deleteMember(member2);
		accountservice.findMember(member2.getId());
		LOGGER.info("deleteMember - end.");
	}
	
	
	@Test
	public void findMemberByName() {
		LOGGER.info("findMemberByName - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Member member2 = accountservice.createMember(member);
		Assert.assertFalse(accountservice.findMemberByName(
				member.getName().substring(0, 2)).isEmpty());
		Member mem = (Member) entityfactory.createDomainEntity("member");
		Member mem2 = accountservice.createMember(mem);
		Assert.assertTrue(accountservice.findMemberByName(
				member.getName().substring(0, 2)).size() == 2);
		Assert.assertTrue(accountservice.findMemberByName("ooooooxxxxxxx").size() == 0);
		accountservice.deleteMember(member2);
		accountservice.deleteMember(mem2);
		LOGGER.info("findMemberByName - end.");
	}
	
	@Test
	public void createCategory() {
		LOGGER.info("createCategory - starting...");
		Category category = (Category) entityfactory.createDomainEntity("category");
		Category category2 = accountservice.createCategory(category);
		Assert.assertNotNull(category2);
		Assert.assertEquals(category.getName(), category2.getName());
		accountservice.deleteCategory(category2);
		LOGGER.info("createCategory - end.");
	}
	
	@Test
	public void findCategory() {
		LOGGER.info("findCategory - starting...");
		Category category = (Category) entityfactory.createDomainEntity("category");
		Category category2 = accountservice.createCategory(category);
		Category category3;
		try {
			category3 = accountservice.findCategory(category2.getId());
			Assert.assertNotNull(category3);
			Assert.assertEquals(category.getName(), category3.getName());
			accountservice.deleteCategory(category2);
		} catch (ElementNotExistException e) {
		}
		LOGGER.info("findCategory - end.");
	}
	
	@Test(expectedExceptions = { ElementNotExistException.class })
	public void deleteCategory() throws ElementNotExistException {
		LOGGER.info("deleteCategory - starting...");
		Category category = (Category) entityfactory.createDomainEntity("category");
		Category category2 = accountservice.createCategory(category);
		accountservice.deleteCategory(category2);
		accountservice.findCategory(category2.getId());
		LOGGER.info("deleteCategory - end.");
	}
	
	@Test
	public void findAllCategories() {
		LOGGER.info("findAllCategories - starting...");
		Category category = (Category) entityfactory.createDomainEntity("category");
		Category category2 = accountservice.createCategory(category);
		Category _category = (Category) entityfactory.createDomainEntity("category");
		Category _category2 = accountservice.createCategory(_category);
		List<Category> list = accountservice.findAllCategories();
		Assert.assertTrue(!list.isEmpty());
		Assert.assertEquals(2, list.size());
		accountservice.deleteCategory(_category2);
		list = accountservice.findAllCategories();
		Assert.assertEquals( list.get(0).getName(), category.getName());
		accountservice.deleteCategory(category2);
		LOGGER.info("findAllCategories - end.");
	}
	
	
	
	
	
	@Test
	public void IdentifyWebClient() {
		LOGGER.info("IdentifyWebClient - starting...");
		Member member = (Member) entityfactory.createDomainEntity("member");
		Assert.assertFalse(accountservice.IdentifyWebClient(member));
		Member member2 = accountservice.createMember(member);
		Assert.assertTrue(accountservice.IdentifyWebClient(member2));
		accountservice.deleteMember(member2); 
		LOGGER.info("IdentifyWebClient - end.");
	}

}
